package easy;

public class Solution_892 {

    public int surfaceArea(int[][] grid) {
        int count = 0;
        int length = grid.length;
        for (int i = 0; i < length; i++) {
            int pre_y = 0, pre_x = 0;
            for (int j = 0; j < length; j++) {
                int c = grid[i][j] > 0 ? (grid[i][j] << 2) + 2 : 0;
                count = count + c - (Math.min(pre_y, grid[i][j]) << 1) - (Math.min(pre_x, grid[j][i]) << 1);
                pre_y = grid[i][j];
                pre_x = grid[j][i];
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Solution_892 model = new Solution_892();
        System.out.println(model.surfaceArea(new int[][]{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}));
        System.out.println(model.surfaceArea(new int[][]{{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}));
    }
}
